<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Create HTML widget with annotations and channels per movie/search
 * @author Matthijs van Henten <matthijs@waag.org>
 *
 *
<video class="currentVideo" controls="true" autoplay="false" tabindex="0">
<source src="/movies/movie_5_DNA_dice_lecture_2.ogv"/>
</video>
*
 */
class Zend_View_Helper_AnnotationBrowser{
	private $_annotations;
	private $_channels;
	private $_movie;

	private $url;

	/**
	 * This function is used as a pseudo constructor by Zend_View
	 * and may be called as a function.
	 *
	 * @param Default_Model_Movie $movie to show
	 * 		The __set method is used to parse the variables
	 *
	 * 	@return string $pager HTML code for the pager
	 */
	public function AnnotationBrowser( array $channels, array $annotations, Default_Model_Movie $movie = null ){
		$this->_annotations = $annotations;
		$this->_channels    = $channels;
		$this->_movie       = $movie;

		return $this->getList();
	}

	/**
	 * Explicit render of the player
	 *
	 * @return string $HTML Html code for this pager
	 */
	public function getList(){
		$html = array();

		if( count( $this->_channels ) == 0 ){
			return '';
		}

		// @todo with inconsistent data, this rendering will display unexpected results
		// We should sort channels -> annotations properly first
		// and only iteraate over the channel array.
		foreach( $this->_annotations as $annotation ){
			$cid 	 = $annotation->channelId;
			$content = $annotation->comment;

			if( !isset( $html[$cid] ) ){
				foreach( $this->_channels as $channel ){
					if( $channel->id == $cid ){
						$html[$cid] = $channel->toArray();
					}
				}
				$html[$cid]['children'] = array();
			}



			if( $html[$cid]['properties']->comment == false ){
				$content = join( ", ", $annotation->getKeywords());
			}


			if( strlen( $content ) > 25 ){
				$content = substr_replace( $content, '...', 22 );
			}

			$c = array();
			$c[] = $this->getElement('span', $annotation->getStart(), array('class' => 'duration') );
			$c[] = $this->getLink( $content, array(
				'options'	=> array( 'action' => 'play', 'id' => $annotation->id ),
				'name'		=> 'annotation'
			),
			array('class'	=> 'floatLeft')
			);
			//$c[] = $this->getElement('p', $content, array('class' => 'contentPreview') );

			if( null !== $this->_movie ){
				$c[] = $this->getLink( 'modify', array(
					'options' => array('action' => 'edit', 'id' => $annotation->id ),
					'name' => 'annotation'
				),
				array('class' => 'floatRight')
				);
			}

			$html[$cid]['children'][] = $this->getElement('li', join("\n", $c ) );
		}

		foreach( $this->_channels as $channel ){
			$children = array();

			if( isset( $html[$channel->id] ) ){
				$children = $html[$channel->id]['children'];
			}

			$ul 	= $this->getElement('ul', join( "\n", $children ) );
			$name   = $channel->name;

			if( null !== $this->_movie ){
				$name = $name . $this->getElement('span', '+', array('class' => 'floatRight') );
				$name = $this->getLink( $name, array(
					'options'	=> array(
						'action' => 'add',
						'id' => $this->_movie->id,
						'channel' => $channel->id
					),
					'name'		=> 'annotation'
				),
				array('class' => 'clear'));
			}

			$title  = $this->getElement('h4', $name );
			$div	= $this->getElement('div', $title . $ul, array('class' => 'channel') );

			$html[$channel->id] = $div;
		}

		return join( "\n", $html );
	}

	/**
	 * render a single HTML string using a variable tagname, content and attributes
	 *
	 * @param string $tagName Something of a HTML tagName such as 'div'
	 * @param string $content Content to put into the tags
	 * @param array $attributes A key_value pair of attributes
	 *
	 * @return string $HTML
	 */
	public function getElement( $tagName, $content, $attributes=array() ){
		foreach( $attributes as $key => $value ){
			$attributes[$key] = sprintf('%s="%s"', $key, htmlspecialchars($value) );
		}

		return sprintf(
			"<%s%s>%s</%s>\n",
			$tagName,
			rtrim( ' ' . join(' ', $attributes) ),
			trim($content),
			$tagName
		);
	}

	/**
	 * wrapper around getElement to make the function call shorter
	 *
	 * @param string $content Content to be wrapped in the link
	 * @param array $href urlOptions for url
	 * @param array $attributes Optional attributes
	 */
	public function getLink( $content, array $href, array $attributes = array() ){
		if( isset( $href['options'] ) ){
			// assume named params
			$template = array('options' => array(), 'name' => null, 'reset' => false, 'encode' => true );
			$href = array_merge( $template, $href );
			$attributes['href'] = call_user_func_array( array( $this, 'url' ), $href );
		}
		else{
			$attributes['href'] = $this->url( $href );
		}

		//$href = $this->url( $href );

		return $this->getElement( 'a', $content, $attributes );
	}

	/**
	 * Forward call to Zend_View_Helper_Url()
	 *
	 * @param array $urlOptions Options to set
	 * @param string $name Name of the named router
	 * @param bool $reset Reset current router (false)
	 * @param bool $encode Use urlencode
	 */
	public function url( array $urlOptions = array(), $name = null, $reset = false, $encode = true ){
		if( null == $this->url ){
			$this->url = new Zend_View_Helper_Url();
		}

		return $this->url->url( $urlOptions, $name, $reset, $encode );
	}
}
